package com.lineage.echo;

import com.lineage.commons.system.LanSecurityManager;
import com.lineage.config.Config;
import com.lineage.config.ConfigIpCheck;
import com.lineage.server.thread.GeneralThreadPool;
import com.lineage.server.utils.StreamUtil;
import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketTimeoutException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class ServerExecutor extends Thread {
  private static final Log _log = LogFactory.getLog(ServerExecutor.class);
  
  private ServerSocket _server;
  
  private int _port = 0;
  
  private static final String _t1 = "\n\r--------------------------------------------------";
  
  private static final String _t2 = "\n\r--------------------------------------------------";
  
  public ServerExecutor(int port) {
    try {
      this._port = port;
      if (!"*".equals(Config.GAME_SERVER_HOST_NAME)) {
        InetAddress inetaddress = InetAddress.getByName(Config.GAME_SERVER_HOST_NAME);
        this._server = new ServerSocket(this._port, 50, inetaddress);
        return;
      } 
      this._server = new ServerSocket(this._port);
      return;
    } catch (SocketTimeoutException e) {
      _log.fatal("连线超时:(" + this._port + ")", e);
    } catch (IOException e2) {
      _log.fatal("IP位置加载错误 或 端口位置已被占用:(" + this._port + ")", e2);
    } finally {
      _log.info("[D] " + getClass().getSimpleName() + " 开始监听服务端口:(" + this._port + ")");
    } 
  }
  
  public void stsrtEcho() throws IOException {
    GeneralThreadPool.get().execute(this);
  }
  
  public void run() {
    String lanInfo;
    try {
      while (this._server != null) {
        Socket socket = null;
        try {
          socket = this._server.accept();
          if (socket == null)
            continue; 
          String ipaddr = socket.getInetAddress().getHostAddress();
          if (ConfigIpCheck.IPCHECKPACK) {
            socket.setSoTimeout(120000);
            if (ConfigIpCheck.IPCHECKPACK) {
              socket.setSoTimeout(1000);
              LanSecurityManager.BANIPPACK.put(ipaddr, Integer.valueOf(30));
            } 
          } 
          String info = "\n\r--------------------------------------------------\n       客户端 连线游戏伺服器 服务端口:(" + 
            this._port + ")" + "\n       " + ipaddr + 
            "\n\r--------------------------------------------------";
          _log.info(info);
          ClientExecutor client = new ClientExecutor(socket);
          GeneralThreadPool.get().execute(client);
        } catch (SecurityException securityException) {}
      } 
    } catch (IOException ex2) {
      return;
    } finally {
      String str = "[D] " + getClass().getSimpleName() + " 服务器核心关闭监听端口(" + this._port + ")";
      _log.warn(str);
    } 
  }
  
  public void stopEcho() {
    try {
      if (this._server != null) {
        StreamUtil.close(this._server);
        StreamUtil.interrupt(this);
        this._server = null;
      } 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
}
